from redis import StrictRedis, ConnectionPool
from utils.read_yaml import YAML
from flask_caching import Cache


class RedisClient:
    def __init__(self):
        self._redis_information = YAML.ReadRoot('redis')
        if self._redis_information:
            self._redis_pool = ConnectionPool(host=self._redis_information['HOST'],
                                              port=self._redis_information['PORT']
                                              )
            if 'USERNAME' in self._redis_information.keys() and 'PASSWORD' in self._redis_information.keys():
                self._redis_pool = ConnectionPool(host=self._redis_information['HOST'],
                                                  port=self._redis_information['PORT'],
                                                  username=self._redis_information['USERNAME'],
                                                  password=self._redis_information['PASSWORD'],
                                                  decode_responses=True
                                                  )
            self.redis_client = StrictRedis(connection_pool=self._redis_pool, decode_responses=True)


_redis_information = YAML.ReadRoot('redis')
config = {
    "CACHE_TYPE": "redis",
    "CACHE_REDIS_HOST": _redis_information['HOST'],  # Default cache time
    'CACHE_REDIS_PORT': _redis_information['PORT']
}

RedisCache = Cache(config=config)
